/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2024-05-03 08:51:34
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-05-03 21:36:32
 * @FilePath: /pipe/processpool/mytask.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once
#include<iostream>
#include<unistd.h>

using namespace std;

typedef void(*work_t)(int);    //函数指针
typedef void(*task_t)(int,pid_t);  //函数指针类型

void PrintLog(int fd,pid_t pid)
{
    cout << "sub process: " << pid << ", fd: " << fd<< ", task is : printf log task\n" << endl;
}

void ReloadConf(int fd,pid_t pid)
{
    cout << "sub process: " << pid << ", fd: " << fd<< ", task is : reload conf task\n" << endl;
}

void ConnectMysql(int fd,pid_t pid)
{
    cout << "sub process: " << pid << ", fd: " << fd<< ", task is : connect mysql task\n" << endl;
}

task_t tasks[3] = {PrintLog, ReloadConf, ConnectMysql};
int NextTask()
{
    return  rand()%3;
}
void worker(int fd)
{
    while(true)
    {
        uint32_t command_code = 0;  //任务下标码
        ssize_t n = read(0, &command_code, sizeof(command_code));   //读取
        if(n == sizeof(command_code))
        {
            if(command_code >= 3) continue;
            tasks[command_code](fd,getpid());
        }
        //cout << "I am worker: " << getpid() << endl;
        else if(n==0)
        {
            std::cout << "sub process: " << getpid() << " quit now..." << std::endl;
            break;
        }
        sleep(1);
    }
}
